package com.hsc.mvc.handler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * @author
 * @date 2025-06-10
 * @Description 重定向处理器
 * 返回的字符串中 包含 redirect 字符的
 */
public class RedirectHandler implements ResponseHandleStrategy {
    @Override
    public boolean isSupport(Object result, Method targetMethod) {
        return result instanceof String && ((String) result).contains("redirect:");
    }

    /**
     * 处理重定向 //result = "redirect:1.jsp"
     *
     * @param result
     * @param targetMethod
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void handle(Object result, Method targetMethod, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // //result = "redirect:1.jsp"
        // 进行转发 处理字符
        String redirect = (String) result;
        redirect = redirect.replace("redirect:", "");
        System.out.println(redirect);
        // 去掉标识字符 并且进行转发
        response.sendRedirect(redirect);
    }
}
